package com.youjiang.spirder.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.youjiang.spirder.constant.Source;
import com.youjiang.spirder.mapper.CoreMapperCustom;
import com.youjiang.spirder.mapper.generate.CategoryMapper;
import com.youjiang.spirder.po.CategoryExample;
import com.youjiang.spirder.po.KuaibaoNewList;
import com.youjiang.spirder.po.TencentNewList;
import com.youjiang.spirder.po.UcToutiaoList;
import com.youjiang.spirder.utils.JsonFormatUtils;
import com.youjiang.spirder.utils.WordUtils;
import com.youjiang.spirder.utils.page.OrderType;
import com.youjiang.spirder.utils.page.Pager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 核心服务层
 * @Company: yimo
 * @author: Kwum
 * @date 2018年2月24日 下午3:35:33
 */

@Service
public class CoreService {
	
	@Resource
	private CategoryMapper categoryMapper;
	@Resource
	private CoreMapperCustom coreMapperCustom;

	/**
	 * 根据平台源获取类别
	 * @param sourceId 平台源（静态类）
	 * @return
	 * @throws Exception
	 * @author Kwum
	 */
	public String selectCategoryBySourceId(Integer sourceId) throws Exception {
		CategoryExample example = new CategoryExample();
		example.createCriteria().andSourceEqualTo(sourceId);
		return JsonFormatUtils.success(JSONArray.fromObject(categoryMapper.selectByExample(example)));
	}

	/**
	 * 获取文章列表
	 * @param sourceId 平台源
	 * @param category 类别
	 * @param keyword 关键字
	 * @param currentPage 当前页码
	 * @param totalCount 当前页所需条目数
	 * @return
	 * @author Kwum
	 */
	public String selectArticleByCategory(Integer sourceId, String category, String keyword, Integer currentPage, Integer totalCount) 
			throws Exception {
		
		//uc头条
		if(sourceId == Source.UC_TOUTIAO.getValue()) {
			Pager<UcToutiaoList> pager = new Pager<>(currentPage, totalCount, "uc.create_time", OrderType.DESC);
	        List<UcToutiaoList> pageList = coreMapperCustom.pageUCToutiaoListByCategory(category, keyword, pager);
	        pager.setList(pageList);
	        JSONObject json = JSONObject.fromObject(pager);
	        return JsonFormatUtils.success(json);
		
		//腾讯新闻
		}else if(sourceId == Source.TENCENT_NEW.getValue()) {
			Pager<TencentNewList> pager = new Pager<>(currentPage, totalCount, "tx.create_time", OrderType.DESC);
	        List<TencentNewList> pageList = coreMapperCustom.pageTencentNewListByCategory(category, keyword, pager);
	        pager.setList(pageList);
	        JSONObject json = JSONObject.fromObject(pager);
	        return JsonFormatUtils.success(json);
	        
	    //天天快报新闻
  		}else if(sourceId == Source.KUAIBAO_NEW.getValue()) {
  			Pager<KuaibaoNewList> pager = new Pager<>(currentPage, totalCount, "kb.create_time", OrderType.DESC);
  	        List<KuaibaoNewList> pageList = coreMapperCustom.pageKuaibaoNewListByCategory(category, keyword, pager);
  	        pager.setList(pageList);
  	        JSONObject json = JSONObject.fromObject(pager);
  	        return JsonFormatUtils.success(json);
  		}else {
			
			return JsonFormatUtils.error(1004);
		}
	}

	public void downloadArtcle(Integer sourceId, String title, String url, HttpServletResponse resp) throws Exception {
		
		//uc头条
		if(sourceId == Source.UC_TOUTIAO.getValue()) {
			WordUtils.exportClient(resp, title, WordUtils.spirdeUCTouTiaoInfoByUrl(url));
			
		//腾讯新闻
		}else if(sourceId == Source.TENCENT_NEW.getValue()) {
			WordUtils.exportClient(resp, title, WordUtils.spirdeTencentNewInfoByUrl(url));
		
		//天天快报
		}else if(sourceId == Source.KUAIBAO_NEW.getValue()) {
			WordUtils.exportClient(resp, title, WordUtils.spirdeKuaiBaoNewInfoByUrl(url));
		}
	}
}
